﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HDJ.Framework.Core
{
    public class ObjectReference : MonoBehaviour {

        public List<NameToObject> referenceList = new List<NameToObject>();

        private Dictionary<string, GameObject> referenceDic = new Dictionary<string, GameObject>();

        private bool isInit = false;
        private void Init()
        {
            if (isInit) return;
            isInit = true;
            for (int i = 0; i < referenceList.Count; i++)
            {
                NameToObject item = referenceList[i];
                try
                {
                    referenceDic.Add(item.name, item.gameObject);
                }
                catch (Exception e)
                {
                    Debug.LogError("Item :" + item.name + "\n" + e);
                }
            }
        }

        public GameObject GetGameObject(string name)
        {
            Init();

            GameObject obj = null;
            if (!referenceDic.TryGetValue(name, out obj))
                Debug.LogError("未找到GameObject ：" + name);
            return obj;
        }

    }

    [System.Serializable]
    public struct NameToObject
    {
        public string name;
        public GameObject gameObject;
    }


}